#include "test_controller.hpp"
#include "service/serv_manage.hpp"
#include "utils/tinyxml2.h"
#include "utils/xml_json.hpp"
#include "wlog.hpp"
#include "db/sqlite_conn.hpp"
#include "device.hpp"
#include "client/xd_client.hpp"

using namespace std;

namespace bz {
	test_controller::test_controller()
	{
		xd_client_ = serv_manage::instance()->main_mcu_;
	}

	async_simple::coro::Lazy<void> test_controller::test_create_cospace(coro_http_request& req, coro_http_response& res)
	{

		
		
		for (int index = 41; index < 81; ++index)
		{

			string ip = "172.16.100.214";

			if (index > 40 && index <= 80) {
				ip = "10.0.66.11";
			}
			if (index > 80 && index <= 120) {
				ip = "172.16.100.75";
			}

			if (index > 120 && index <= 160) {
				ip = "172.16.100.131";
			}
			if (index > 160 && index <= 200) {
				ip = "172.16.100.33";
			}


			std::string uri = "https://"+ip+":9443/api/v1/coSpaces";

			coro_http_client client{};
			client.add_header("Authorization", "Basic YWRtaW46UEByYWQxc2U=");
			client.set_conn_timeout(std::chrono::seconds(3));
			client.set_req_timeout(std::chrono::seconds(3));
			int room =75000+ index;
			string MeetingNumber = to_string(room);

			string param = "name=" + MeetingNumber + "&uri=" + MeetingNumber + "&callId=" + MeetingNumber;
			auto http_res = co_await client.async_post(uri, param, req_content_type::string);

			WLOG << "http_res stataus =" << http_res.status << " body=" << http_res.resp_body;
			client.reset();
			co_await async_simple::coro::sleep(200ms);
		}
		
		res.set_status_and_content(status_type::ok, success_msg().dump());
		co_return;
	}

	async_simple::coro::Lazy<void> clearCall(string ip)
	{
		coro_http_client client{};
		
		client.reset();
		client.add_header("Authorization", "Basic YWRtaW46UEByYWQxc2U=");
		client.set_conn_timeout(std::chrono::seconds(3));
		client.set_req_timeout(std::chrono::seconds(3));
		std::string uri = "https://" + ip + ":9443/api/v1/calls";
		auto http_res = co_await client.async_get(uri);

		if (http_res.status) {
			string data(http_res.resp_body);
			//WLOG << "clearCall res data= " << data;
			
			auto root = xml_to_json(data, { "calls" });

			WLOG << "root json = " << root.dump(4);
		}

		co_return;
	}

	async_simple::coro::Lazy<void> test_controller::clear_all_calls(coro_http_request& req, coro_http_response& res)
	{

		/*for (int i = 2; i <= 3; ++i)
		{
			string ip = "172.16.3." + std::to_string(i);
			co_await clearCall(ip);
		}*/
		co_await clearCall("172.16.100.33");
		res.set_status_and_content(status_type::ok, success_msg().dump());
		co_return;
	}

	async_simple::coro::Lazy<void> test_controller::db_test1(coro_http_request& req, coro_http_response& res)
	{
		auto db = sqlite_conn::get_conn();
		sqlite_guard guard(db);
		vector<device> all_device = db->query(FID(device::id), "=", 118);
		
		co_await async_simple::coro::sleep(3s);
		guard.unlock();
		res.set_status_and_content(status_type::ok, success_msg(all_device).dump());
		co_return;
	}

	async_simple::coro::Lazy<void> test_controller::db_test2(coro_http_request& req, coro_http_response& res)
	{
		auto db = sqlite_conn::get_conn();
		
		if (db) {
			sqlite_guard guard(db);
			vector<device> all_device = db->query(FID(device::id), "=", 118);
			res.set_status_and_content(status_type::ok, success_msg(all_device).dump());
		}
		else {
			res.set_status_and_content(status_type::ok, success_msg().dump());
		}
		
		
		co_return;
	}

	async_simple::coro::Lazy<void> test_controller::on_publish(coro_http_request& req, coro_http_response& res)
	{
		auto body = req.get_body();

		WLOG << "on_pulish body = " << body;
		json root = {
			{"code",0},
			{"msg","OK"}
		};
		res.set_status_and_content(status_type::ok, root.dump());
		co_return;
	}

	async_simple::coro::Lazy<void> test_controller::xd_test(coro_http_request& req, coro_http_response& res)
	{
		
		//auto arr  = xd.list_template();
		//auto arr  = xd.list_tasks();
		//auto arr  = xd_client_->startMeet("c7788");
		res.set_status_and_content(status_type::ok, success_msg().dump());
		co_return;
	}

	async_simple::coro::Lazy<void> test_controller::xd_create_temp(coro_http_request& req, coro_http_response& res)
	{
		
		
		auto arr = xd_client_->addTemplate("c7788","83365","","1920x1080",60, 1920000);
		res.set_status_and_content(status_type::ok, success_msg(arr.ret).dump());
		co_return;
	}

	async_simple::coro::Lazy<void> test_controller::xd_device_list(coro_http_request& req, coro_http_response& res)
	{
		auto arr = xd_client_->list_device();
		res.set_status_and_content(status_type::ok, success_msg(arr.second).dump());
		co_return;
	}

	async_simple::coro::Lazy<void> test_controller::xd_clear_tmp(coro_http_request& req, coro_http_response& res)
	{
		auto template_list = xd_client_->list_template();
		
		for (auto it : template_list.second)
		{
			auto ret = xd_client_->del_template(it);
			WLOG << "del name =" << it << " ret=" << ret.ret;
		}
		res.set_status_and_content(status_type::ok, success_msg().dump());
		co_return;
	}


	
}